The REPEAT statement, like the
WHILE statement, executes the same script section repeatedly until its control expression evaluates to
FALSE. Unlike the
WHILE statement, however, the
REPEAT statement evaluates the control expression after executing its controlled statement. This means that the controlled statement will always execute at least once.
The example from the WHILE statement section could easily be rewritten using a
REPEAT statement:
In this format, the statements within the REPEAT-UNTIL structure would be executed at least once, whether or not
h was initially
NIL, which could cause detrimental effects or errors. Generally speaking,
REPEAT statements should be used in conditions where executing the controlled statement will not have a negative impact.
WHILE statements are most useful when the condition controlling their execution may have already been satisfied;
REPEAT statements, on the other hand, are most useful when the condition can be satisfied only by executing the statement.
Also note that REPEAT statements do not require the use of
BEGIN or
END, as the
REPEAT and
UNTIL keywords create their own compound statement out of the statements between them.